package com.sd.repay.service.impl;


import com.sd.repay.common.Constants;
import com.sd.repay.dao.BaseDao;
import com.sd.repay.service.AgentService;
import com.sd.repay.service.MerFeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by sd on 2018/08/21
 * 代理商服务类
 */
@Service
public class AgentServiceImpl implements AgentService {

    public final static Logger log = LoggerFactory.getLogger(AgentServiceImpl.class);

    @Autowired
    private BaseDao baseDao;
    @Autowired
    private MerFeeService merFeeService;

    /**
     * 保存代理商信息
     *
     * @param agentInfoMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public int addAgentInfo(Map<String, String> agentInfoMap) throws Exception {

        String agentNo = agentInfoMap.get("agentNo");
        String mobileNo = agentInfoMap.get("mobileNo");
        String oemType = agentInfoMap.get("oemType");
        String oneAgentNo = agentInfoMap.get("oneAgentNo");
        String agentLevel = agentInfoMap.get("agentLevel");

        String sql = " INSERT INTO t_agent_info(agent_no,agent_name,agent_node,agent_level,parent_no,one_agent_no,oem_type,mobile_no,login_pwd,email,link_man," +
                "id_card_no,province,city,address,custom_tel,status,create_time) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        List<Object> params = new ArrayList<>();
        params.add(agentNo);
        params.add(agentInfoMap.get("agentName"));
        params.add(agentInfoMap.get("agentNode"));
        params.add(agentInfoMap.get("agentLevel"));
        params.add(agentInfoMap.get("parentNo"));
        params.add(oneAgentNo);
        params.add(oemType);
        params.add(mobileNo);
        params.add(agentInfoMap.get("loginPwd"));
        params.add(agentInfoMap.get("email"));
        params.add(agentInfoMap.get("linkMan"));
        params.add(agentInfoMap.get("idCardNo"));
        params.add(agentInfoMap.get("province"));
        params.add(agentInfoMap.get("city"));
        params.add(agentInfoMap.get("address"));
        params.add(agentInfoMap.get("customTel"));
        params.add(Constants.AGENT_STATUS_SUCCESS);
        params.add(new Date());

        int count = baseDao.execute(sql, params.toArray());
        log.info("代理商手机号：{}新增代理商编号：{}影响的行数：{}", new Object[]{mobileNo, agentNo, count});

        // TODO: 2018/9/3 代理商成本如何设定取值，后续讨论确定
        if (count > 0) {
            //设置代理商成本信息
            Map<String, String> costParams = new HashMap<>();
            costParams.put("oemType", oemType);
            costParams.put("merType", Constants.MER_TYPE_AGENT);
            costParams.put("merNo", agentNo);
            costParams.put("quickFeeRate", "0.0055");
            costParams.put("quickSingleFee", "1");
            costParams.put("perfectFeeRate", "0.0053");
            costParams.put("perfectSingleFee", "1");
            costParams.put("withdrawFeeRate", "0");
            costParams.put("withdrawSingleFee", "2");

            count = merFeeService.saveMerFeeInfo(costParams);
            log.info("代理商手机号：{}新增代理商编号：{}设置成本影响的行数：{}", new Object[]{mobileNo, agentNo, count});
            if (count < 1) {
                throw new RuntimeException("代理商手机号：" + mobileNo + "保存代理商失败");
            }
        }
        return count;
    }

    /**
     * 根据代理商编号获取代理商信息
     *
     * @param agentNo
     * @return
     */
    @Override
    public Map<String, Object> getAgentInfoByAgentNo(String agentNo) throws Exception {

        String sql = " SELECT * FROM t_agent_info WHERE agent_no = ? ";
        return baseDao.findFirst(sql, agentNo);
    }

    /**
     * 根据手机号和OEM获取代理商对象
     *
     * @param mobileNo
     * @param oemType
     * @return
     */
    @Override
    public Map<String, Object> getAgentInfoByMobileAndOem(String mobileNo, String oemType) throws Exception {

        String sql = " SELECT * FROM t_agent_info WHERE mobile_no = ? AND oem_type = ? ";
        return baseDao.findFirst(sql, new Object[]{mobileNo, oemType});
    }
}
